//
//  LKNewVisitRecordViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/14.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKNewVisitRecordViewController.h"
#import "LKMyDescriptionCell.h"
#import "LKWriteContinuInvestCell.h"
#import "LKMyChooseCustomerViewController.h"
#import "LKVisitRecordViewController.h"
#import "GFDatePicker.h"
#import "LKVisitModel.h"
#import "LKCustomerModel.h"
@interface LKNewVisitRecordViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSArray *titlesArray;
@property(nonatomic,copy)NSString *visitContent;
@property(nonatomic,copy)NSString *visittime;
@property(nonatomic,strong)NSMutableArray *idsArray;
@end

@implementation LKNewVisitRecordViewController


-(LKBaseTableview *)tableView{
    if (!_tableView) {
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 12 + NAVIGATION_BAR_HEIGHT, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 12)) style:UITableViewStyleGrouped registercellClasss:@[@"LKWriteContinuInvestCell",@"LKMyDescriptionCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 200;
    // Do any additional setup after loading the view.
}
-(void)setNavgationBackBtn{
    WS(weakSelf)
    [super setNavgationBackBtn];
    self.navigationItem.title = @"新建拜访记录";
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.titleLabel.font = [UIFont systemFontOfSize:16];
    [btn setTitle:@"确定" forState:UIControlStateNormal];
    [btn setTitleColor:blueDefault forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf createNewVisit];
    }];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf)
    if (indexPath.section == 0) {
        LKMyDescriptionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKMyDescriptionCell"];
        cell.titleLabel.text = self.titlesArray[indexPath.section];
        [[cell.textView rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
            weakSelf.visitContent = x;
        }];
        return cell;
    }
    LKWriteContinuInvestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKWriteContinuInvestCell"];
    cell.titleLabel.text = self.titlesArray[indexPath.section];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return indexPath.section == 0 ? 226 : 43;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0000000000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 12;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    WS(weakSelf)
    switch (indexPath.section) {
        case 0:
            {
                
            }
            break;
        case 1:
        {
            LKMyChooseCustomerViewController *vc = [[LKMyChooseCustomerViewController alloc] init];
            vc.customersBlcok = ^(NSArray *arr) {
                weakSelf.idsArray = (NSMutableArray *)arr;
                LKWriteContinuInvestCell *cell = (LKWriteContinuInvestCell *)[tableView cellForRowAtIndexPath:indexPath];
                NSString *nameStr = @"";
                for (NSInteger i = 0; i < self.idsArray.count; i++) {
                    LKCustomerModel *model = self.idsArray[i];
                    nameStr = [nameStr stringByAppendingString:[NSString stringWithFormat:@" %@",model.customerName]];
                }
                cell.contentLabel.text = nameStr;
            };
            
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 2:
        {
            [GFDatePicker showCustomDatePickerAtView:[UIApplication sharedApplication].keyWindow choosedDateBlock:^(NSDate *date) {
                NSString *dateStr = [NSString stringWithFormat:@"%@",date];
                NSString *timeStr = [dateStr componentsSeparatedByString:@" "][0];
                LKWriteContinuInvestCell *cell = (LKWriteContinuInvestCell *)[tableView cellForRowAtIndexPath:indexPath];
                cell.contentLabel.text = timeStr;
                weakSelf.visittime = timeStr;
            } cancelBlock:^{
                
            }];
        }
            break;
        default:
            break;
    }
}
-(NSArray *)titlesArray{
    if (!_titlesArray) {
        _titlesArray = @[@"输入拜访总结",@"选择客户",@"拜访时间"];
    }
    return _titlesArray;
}

//创建新的拜访记录
-(void)createNewVisit{
    NSMutableArray *idsArra = [[NSMutableArray alloc] init];
    for (LKCustomerModel *model in self.idsArray) {
        [idsArra addObject:model.customerId];
    }
    NSMutableDictionary *mutableDic = [[NSMutableDictionary alloc] init];
    NSDictionary *param1 = [NSDictionary dictionaryWithObjectsAndKeys:self.visitContent,@"VisitContent",self.visittime,@"VisitTime", nil];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:idsArra,@"CustomerIds", nil];
    [mutableDic addEntriesFromDictionary:param1];
    [mutableDic addEntriesFromDictionary:param];
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:addNewVisitUrl header:[LKUserInforTool header] parameters:mutableDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        LKVisitRecordViewController *vc = [[LKVisitRecordViewController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(NSMutableArray *)idsArray{
    if (!_idsArray) {
        _idsArray = [[NSMutableArray alloc] init];
    }
    return _idsArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
